<?php
/******************************
 * EQdkp
 * Copyright 2002-2005
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * listitemrequest.php
 * Began: November 2008
 * 
 * 
 ******************************/
 
define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

$user->check_auth('u_item_list');

//
// Item Request History (all items)
//
if ( $_GET[URI_PAGE] == 'history' )
{
    $sort_order = array(
	0 => array('request_fulfilled', 'request_fulfilled desc'),
        1 => array('item_requester', 'item_requester desc'),
        2 => array('item_name', 'item_name desc'),
        3 => array('id.dropped_from', 'id.dropped_from desc')
    );
    
    $current_order = switch_order($sort_order);

    $u_list_items = 'listitemrequest.php'.$SID.'&amp;' . URI_PAGE . '=history&amp;';
    
    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listpurchased_title'];
    
    $total_items = $db->query_first('SELECT count(*) FROM ' . ITEM_REQUEST_TABLE);
    $start = ( isset($_GET['start']) ) ? $_GET['start'] : 0;
    
    $s_history = true;
    
    $sql = 'SELECT ir.item_id, 
                   ir.item_name, 
                   ir.item_requester, 
                   ir.request_fulfilled as request_fulfilled,
                   id.dropped_from as dropped_from,
                   id.item_data_id as data_id
	    FROM ' . ITEM_REQUEST_TABLE . ' ir
            LEFT JOIN ' . ITEM_DATA_TABLE . ' id on ir.item_name = id.item_name
	    ORDER BY '.$current_order['sql']. '
	    LIMIT '.$start.','.$user->data['user_ilimit'];

    $listitems_footcount = sprintf($user->lang['listpurchased_footcount'], $total_items, $user->data['user_ilimit']);
    $pagination = generate_pagination('listitemrequest.php'.$SID.'&amp;' . URI_PAGE . '=history&amp;o='.$current_order['uri']['current'], 
                                       $total_items, $user->data['user_ilimit'], $start);
}

//
// Open Item Requests (not fulfilled)
//
else
{
    $sort_order = array(
        0 => array('request_fulfilled', 'request_fulfilled desc'),
        1 => array('item_requester', 'item_requester desc'),
        2 => array('item_name', 'item_name desc'),
        3 => array('id.dropped_from', 'id.dropped_from desc')
    );

    $current_order = switch_order($sort_order);

    $u_list_items = 'listitemrequest.php'.$SID.'&amp;';

    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listpurchased_title'];

    $total_items = $db->query_first('SELECT count(*) FROM ' . ITEM_REQUEST_TABLE . ' WHERE request_fulfilled = 0');
    $start = ( isset($_GET['start']) ) ? $_GET['start'] : 0;

    $s_history = true;

    $sql = 'SELECT ir.item_id as item_id, 
                   ir.item_name as item_name, 
                   ir.item_requester as item_requester, 
                   ir.request_fulfilled as request_fulfilled, 
                   id.dropped_from as dropped_from,
                   id.item_data_id as data_id
            FROM ' . ITEM_REQUEST_TABLE . ' ir
            LEFT JOIN ' . ITEM_DATA_TABLE . ' id on ir.item_name = id.item_name
	    WHERE request_fulfilled = 0
            ORDER BY '.$current_order['sql']. '
            LIMIT '.$start.','.$user->data['user_ilimit'];
 
    $listitems_footcount = sprintf($user->lang['listpurchased_footcount'], $total_items, $user->data['user_ilimit']);
    $pagination = generate_pagination('listitemrequest.php'.$SID.'&amp;' . URI_PAGE . '=history&amp;o='.$current_order['uri']['current'],
                                       $total_items, $user->data['user_ilimit'], $start);

}

// Regardless of which listitem page they're on, we're essentially 
// outputting the same stuff. Request History just has a buyer column.
if ( !($items_result = $db->query($sql)) )
{
    message_die('Could not obtain item request information', '', __FILE__, __LINE__, $sql);
}

while ( $item = $db->fetch_record($items_result) )
{
    $tpl->assign_block_vars('items_row', array(
        'ROW_CLASS' => $eqdkp->switch_row_class(),
        'DROPPED_FROM' => (!empty($item['dropped_from']) ? $item['dropped_from'] : ''),
        'DATA_ID' => (!empty($item['data_id']) ? $item['data_id'] : '-1'),
        'REQUESTER' => ( !empty($item['item_requester']) ) ? $item['item_requester'] : '&lt;<i>Not Found</i>&gt;',
        'U_VIEW_REQUESTER' => 'viewmember.php'.$SID.'&amp;' . URI_NAME . '='.$item['item_requester'],
        'U_VIEW_ITEM' => 'viewitemrequest.php'.$SID.'&amp;' . URI_ITEM . '='.$item['item_id'].'&amp;'.URI_PAGE.'='.$_GET[URI_PAGE].'&amp;',
	'REQUEST_FULFILLED' => ($item['request_fulfilled'] == 1) ? 'Yes' : 'No',
        'NAME' => stripslashes($item['item_name']))
    );
}
$db->free_result($items_result);

$tpl->assign_vars(array(
    'L_REQUEST_FULFILLED' => $user->lang['req_fulfilled'],
    'L_REQUESTER' => $user->lang['requester'],
    'L_ITEM' => $user->lang['item'],
    'L_DROPPED_FROM' => $user->lang['dropped_from'],
    
    'O_REQUEST_FULFILLED' => $current_order['uri'][0],
    'O_REQUESTER' => $current_order['uri'][1],
    'O_NAME' => $current_order['uri'][2],
    'O_DROPPED_FROM' => $current_order['uri'][3],
    
    'U_LIST_ITEMS' => $u_list_items,
    
    'START' => $start,
    'S_HISTORY' => $s_history,
    'LISTITEMS_FOOTCOUNT' => $listitems_footcount,
    'ITEM_PAGINATION' => $pagination)
);

$eqdkp->set_vars(array(
    'page_title'    => $page_title,
    'template_file' => 'listitemrequest.html',
    'display'       => true)
);
?>
